<?php

/**
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once dirname(__FILE__) . '/../../../libs/Mailer/PHPMailerAutoload.php';

class SendMail {

    private $_mail;

    public function __construct() {
        $this->_mail = new PHPMailer;
        self::_setSMTP();
    }

    /**
     * Set SMTP to mail
     */
    private function _setSMTP() {
        require_once dirname(__FILE__) . '/.././Config/Mail.php';
        require_once dirname(__FILE__) . '/.././Common/Const.php';
        // Enable verbose debug output
        if (MAIL_DEBUG) {
            $this->_mail->SMTPDebug = MAIL_DEBUG_VALUE;
        }
        $this->_mail->isSMTP();                                // Set mailer to use SMTP
        $this->_mail->Host = MAIL_HOST;                        // Specify main and backup SMTP servers
        $this->_mail->SMTPAuth = MAIL_AUTH;                    // Enable SMTP authentication
        $this->_mail->Username = MAIL_USER_NAME;               // SMTP username
        $this->_mail->Password = MAIL_PASSWORD;                // SMTP password
        $this->_mail->SMTPSecure = MAIL_SMTP_SECURE;           // Enable TLS encryption, `ssl` also accepted
        $this->_mail->Port = MAIL_PORT;
    }

    /**
     *  Set Header Mail
     * @param string $emailTo
     * @param string $nameTo
     * @param string $replyTo
     * @param string $emailCC
     * @param string $emailBcc
     */
    private function _setMailHeader($emailTo, $nameTo = '', $replyTo = '', $emailCC = '', $emailBcc = '') {
        $this->_mail->From = MAIL_FROM;
        $this->_mail->FromName = MAIL_FROM_NAME;
        $this->_mail->addAddress($emailTo, $nameTo);     // Add a recipient
        if ($replyTo != '') {
            $this->_mail->addReplyTo($replyTo);
        }
        if ($emailCC != '') {
            $this->_mail->addCC($emailCC);
        }
        if ($emailBcc) {
            $this->_mail->addBCC($emailBcc);
        }
    }

    /**
     * Set Content Mail
     * @param string $subject
     * @param string $mailBody
     * @param string $altBody
     * @param string $attach
     */
    private function _setMailContent($subject, $mailBody, $altBody = '', $attach = '') {

        $this->_mail->WordWrap = MAIL_WORDWRAP;                   // Set word wrap to 50 characters
        if ($attach != '') {
            $this->_mail->addAttachment($attach);         // Add attachments
        }
        $this->_mail->isHTML(MAIL_HTML);                          // Set email format to HTML

        $this->_mail->Subject = $subject;
        $this->_mail->Body = $mailBody;
        $this->_mail->AltBody = $altBody;
    }

    /**
     * Send mail
     * @param string $emailTo
     * @param string $subject
     * @param string $mailBody
     * @param string $nameTo
     * @param string $altBody
     * @param string $attach
     * @param string $replyTo
     * @param string $emailCC
     * @param string $emailBcc
     * @return boolean: true, false
     */
    public function sendMail($emailTo, $subject, $mailBody, $nameTo = '', $altBody = '', $attach = '', $replyTo = '', $emailCC = '', $emailBcc = '') {

        self::_setMailHeader($emailTo, $nameTo, $replyTo, $emailCC, $emailBcc);
        self::_setMailContent($subject, $mailBody, $altBody, $attach);
        if (!$this->_mail->send()) {
            return false;
        } else {
            return true;
        }
    }

}
